<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
          <el-form-item label="锁别名" prop="lockAlias">
        <el-input v-model="dataForm.lockAlias" placeholder="锁别名"></el-input>
      </el-form-item>
          <!-- <el-form-item label="锁id" prop="lockId">
        <el-input v-model="dataForm.lockId" placeholder="锁id"></el-input>
      </el-form-item> -->
          <el-form-item label="锁的蓝牙名称" prop="lockName">
        <el-input v-model="dataForm.lockName" placeholder="锁的蓝牙名称"></el-input>
      </el-form-item>
          <el-form-item label="锁mac地址" prop="lockMac">
        <el-input v-model="dataForm.lockMac" placeholder="锁mac地址"></el-input>
      </el-form-item>
          <el-form-item label="锁电量" prop="electricQuantity">
        <el-input v-model="dataForm.electricQuantity" placeholder="锁电量"></el-input>
      </el-form-item>
          <el-form-item label="锁特征值，用于表示锁支持的功能" prop="featureValue">
        <el-input v-model="dataForm.featureValue" placeholder="锁特征值，用于表示锁支持的功能"></el-input>
      </el-form-item>
          <el-form-item label="是否有连接网关，1-是、0-否" prop="hasGateway">
        <el-input v-model="dataForm.hasGateway" placeholder="是否有连接网关，1-是、0-否"></el-input>
      </el-form-item>
          <el-form-item label="锁数据，用于操作锁" prop="lockData">
        <el-input v-model="dataForm.lockData" placeholder="锁数据，用于操作锁"></el-input>
      </el-form-item>
          <el-form-item label="锁初始化时间" prop="date">
        <el-input v-model="dataForm.date" placeholder="锁初始化时间"></el-input>
      </el-form-item>
          <el-form-item label="钥匙状态值" prop="keyStatus">
        <el-input v-model="dataForm.keyStatus" placeholder="钥匙状态值"></el-input>
      </el-form-item>
          <el-form-item label="钥匙id" prop="keyId">
        <el-input v-model="dataForm.keyId" placeholder="钥匙id"></el-input>
      </el-form-item>
          <el-form-item label="" prop="deletePwd">
        <el-input v-model="dataForm.deletePwd" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="wirelessKeypadFeatureValue">
        <el-input v-model="dataForm.wirelessKeypadFeatureValue" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="keyRight">
        <el-input v-model="dataForm.keyRight" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="specialValue">
        <el-input v-model="dataForm.specialValue" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="keyName">
        <el-input v-model="dataForm.keyName" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="noKeyPwd">
        <el-input v-model="dataForm.noKeyPwd" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="passageMode">
        <el-input v-model="dataForm.passageMode" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="timezoneRawOffset">
        <el-input v-model="dataForm.timezoneRawOffset" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="keyboardPwdPersion">
        <el-input v-model="dataForm.keyboardPwdPersion" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="remoteEnable">
        <el-input v-model="dataForm.remoteEnable" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="userType">
        <el-input v-model="dataForm.userType" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="startDate">
        <el-input v-model="dataForm.startDate" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="endDate">
        <el-input v-model="dataForm.endDate" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="备注" prop="remarks">
        <el-input v-model="dataForm.remarks" placeholder="备注"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: '',  lockAlias: '',  lockId: '',  lockName: '',  lockMac: '',  electricQuantity: '',  featureValue: '',  hasGateway: '',  lockData: '',  date: '',  keyStatus: '',  keyId: '',  deletePwd: '',  wirelessKeypadFeatureValue: '',  keyRight: '',  specialValue: '',  keyName: '',  noKeyPwd: '',  passageMode: '',  timezoneRawOffset: '',  keyboardPwdPersion: '',  remoteEnable: '',  userType: '',  startDate: '',  endDate: '',  remarks: ''});

const rules = ref({
          lockAlias: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          lockId: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          lockName: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          lockMac: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          electricQuantity: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          featureValue: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          hasGateway: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          lockData: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          date: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          keyStatus: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          keyId: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          deletePwd: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          wirelessKeypadFeatureValue: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          keyRight: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          specialValue: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          keyName: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          noKeyPwd: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          passageMode: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          timezoneRawOffset: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          keyboardPwdPersion: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          remoteEnable: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          userType: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          startDate: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          endDate: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          remarks: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ]
  });

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/biz/tongtong/lock/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/biz/tongtong/lock", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
